package ui.map 
{
	import com.gaiaframework.api.Gaia;
	import com.greensock.TweenMax;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Point;
	import pages.Pages;
	
	/**
	 * ...
	 * @author usual
	 */
	public class Canal extends MovieClip 
	{
		private var _canalMap:CanalMsk;
		private var _numOctaves:int = 20;
		private var _offset0:Point = new Point();
		private var _offset1:Point = new Point();

		private var _bmpData:BitmapData;
		private var _bmp:Bitmap;

		public function Canal() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
			addEventListener(Event.REMOVED_FROM_STAGE, _removedFromStage);
		}
		
		public function stopEffect():void 
		{
			trace("[Canal.stopEffect] ");
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addMask();
			addEffect();
		}
		
		private function addEffect():void 
		{
			_offset0 = new Point();
			_offset1 = new Point();
			_bmpData = new BitmapData(320,240,false,0);
			_bmp = new Bitmap(_bmpData);
			_bmp.smoothing=true;
			_canalMap.addChild(_bmp);
			_bmp.width = stage.stageWidth;
			_bmp.height = stage.stageHeight;
			TweenMax.to(_bmp, 0, { colorMatrixFilter: { colorize:0x33FFFE, brightness:2, contrast:4 }} );
			this.cacheAsBitmap = true;
			this.mouseEnabled = false;
			_bmp.mask = _canalMap.mask_mc;
			//stage.addEventListener(Event.ENTER_FRAME, _loop);
			TweenMax.delayedCall(.2, _loop);
		}
		
		public function resize(e:Event=null):void 
		{
			if (_bmp && stage)
			{
				var pnx:Number = _bmp.parent.parent.parent.x;
				var pny:Number = _bmp.parent.parent.parent.y;
				var xpos:Number = pnx;
				var ypos:Number = pny
				xpos = (xpos<0) ? (-xpos) : xpos;
				ypos = (ypos<0) ? (-ypos) : ypos;
				_bmp.width = stage.stageWidth;
				_bmp.height = stage.stageHeight;
				_bmp.x = xpos / _bmp.parent.parent.parent.scaleX;
				_bmp.y = ypos / _bmp.parent.parent.parent.scaleX;
			}
		}
		
		
		private function _loop(e:Event=null):void 
		{
			_offset0.y +=  .5;
			_offset0.x +=  .5;
			_offset1.y -=  .5;
			_offset1.x -=  .5;
			_bmpData.perlinNoise(150, 150, _numOctaves, 1, true, false, 2, false, [_offset0, _offset1]);
			//var arr:Array = [_offset0, _offset1];
			//TweenMax.to(_bmpData.perlinNoise, .5, {baseX:150, baseY:150, numOctaves:_numOctaves, randomSeed:1, stitch:true, fractalNoise:false, channelOptions:2, grayScale:false, offsets:arr } );
			TweenMax.delayedCall(.2, _loop);
			//trace("[Canal._loop] ");
		}
		
		private function addMask():void 
		{
			_canalMap = new CanalMsk();
			addChild(_canalMap);
		}
		
		private function _removedFromStage(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _removedFromStage);
			TweenMax.killDelayedCallsTo(_loop);
			//this.removeEventListener(Event.ENTER_FRAME,_loop);
			trace("[Canal._removedFromStage] ");
		}
		
	}

}